/*
 * This file is part of the xOperator SPARQL/XMPP agent.
 * For further information see: http://xoperator.aksw.org
 * Copyright (C) 2007-2008  Jörg Unbehauen

 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.aksw.xoperator.aiml;

import java.util.HashMap;
import java.util.Map;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.packet.Message;

import bitoflife.chatterbean.AliceBot;
import bitoflife.chatterbean.text.Request;


/**
 * Wrapper arounf the AliceBot to enable the external tag in AIML files.
 *
  */
public class AliceBotExternal extends AliceBot {
    private Map<String, ExternalListener> externals = new HashMap<String, ExternalListener>();

    /**
     * Adds an ExternalListener to the bot.
     * 
     * @param listener a Listener for the external tag
     */
    public void registerExternalListener(ExternalListener listener) {
        this.externals.put(listener.getName(), listener);
    }

    /**
     * Removes an ExternalListener from the bot.
     *
     * @param listener the listener to remove
     */
    public void removeExternalListener(ExternalListener listener) {
        this.externals.remove(listener.getName());
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public Map<String, ExternalListener> getExternalListeners() {
        return this.externals;
    }

    /**
     * DOCUMENT ME!
     *
     * @param name DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public ExternalListener getListener(String name) {
        return this.externals.get(name);
    }

    /**
     * DOCUMENT ME!
     *
     * @param req DOCUMENT ME!
     * @param chat DOCUMENT ME!
     * @param message DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public String respond(String req, Chat chat, Message message) {
        ChatContext ccon = (ChatContext) getContext();
        ccon.setChat(chat);
        ccon.setMessage(message);

        String res = super.respond(new Request(req)).trimOriginal();

        return res;
    }

    /**
     * DOCUMENT ME!
     *
     * @param req DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    @Override
    public String respond(String req) {
        throw new UnsupportedOperationException("Do not this method!");
    }
}
